﻿using MeetingRoomBooking.Application.Common.Interfaces;
using MeetingRoomBooking.Application.Common.Mappings;
using MeetingRoomBooking.Application.Common.Models;
using MeetingRoomBooking.Application.Common.Security;

namespace MeetingRoomBooking.Application.Rooms.Queries;

public record GetRoomsWithPaginationQuery : IRequest<PaginatedList<RoomViewModal>>
{
    public int PageNumber { get; init; } = 1;
    public int PageSize { get; init; } = 10;
}
[Authorize]
public class GetRoomsWithPaginationQueryHander : IRequestHandler<GetRoomsWithPaginationQuery, PaginatedList<RoomViewModal>>
{
    private readonly IApplicationDbContext _context;

    public GetRoomsWithPaginationQueryHander(IApplicationDbContext context)
    {
        _context = context;
    }
    public async Task<PaginatedList<RoomViewModal>> Handle(GetRoomsWithPaginationQuery request, CancellationToken cancellationToken)
    {
        var rooms = await _context.Rooms
           .OrderBy(x => x.Id)
           .Select(x => new RoomViewModal
           {
               Name = x.Name,
               Id = x.Id,
               Capacity = x.Capacity,
               Description = x.Description,
               Created = x.Created,
               CreatedBy = x.CreatedBy,
               LastModified = x.LastModified,
               LastModifiedBy = x.LastModifiedBy
           })
           .PaginatedListAsync(request.PageNumber, request.PageSize);

        return rooms;
    }
}
